<?php
//占位
//实质性操作
trait trait_gdimg
{
//-------------------------------------------------------------------------------
	//取局部
	function getbyxy($x, $y, $x2 = '', $y2 = '')
	{
		return $this->getPortionxy($x, $y, $x2, $y2);
	}
//-------------------------------------------------------------------------------

	//取局部
	function getPortion($x, $y, $w, $h)
	{
		$this->getPortionxy($x, $y, $w ? ($x+$w) : 0, $y ? ($y+$h) : 0);
	}
//-------------------------------------------------------------------------------
	//取局部
	function getPortionxy($x, $y, $x2 = '', $y2 = '')
	{
		if(!$x2 || $x2 > $this->sx) $x2 = $this->sx;
		if(!$y2 || $y2 > $this->sy) $y2 = $this->sy;
		
		$w = $x2 - $x + 1;
		$h = $y2 - $y + 1;
		
		$handle = imagecreatetruecolor($w, $h);
		imagecopyresampled($handle, $this->handle, 0, 0, $x, $y, $w, $h, $w, $h);
		$this->init($handle);
		$this->allClear();
		
	}
//-------------------------------------------------------------------------------

	//换颜色
	function replaceColor($color, $replace, $range = 20)
	{
		$this->allColor();
		$array = $this->likeColor($color, $range);
		$replace = $this->color2index($replace);
		
		for($i = 0; $i < $this->sw; $i ++)
		{
			for($j = 0; $j < $this->sh; $j ++)
			{
				if(isset($array[$this->all[$i][$j]]))
				{
					imagesetpixel($this->handle, $i, $j, $replace);
				}
			}
		}
		$this->allClear();
	}
//-------------------------------------------------------------------------------

	function replaceColorNo($color, $replace, $range = 5)
	{
		$this->allColor();
		$array = $this->likeColor($color, $range);
		$replace = $this->color2index($replace);
				
		for($i = 0; $i < $this->sw; $i ++)
		{
			for($j = 0; $j < $this->sh; $j ++)
			{				
				if(!isset($array[$this->all[$i][$j]]))
				{
					imagesetpixel($this->handle, $i, $j, $replace);
				}
			}
		}
		$this->allClear();
	}
//-------------------------------------------------------------------------------
	function fillAll($color = 'fff')
	{
		$this->fill(0, 0, $this->sw, $this->sh, $color);
	}
//-------------------------------------------------------------------------------

	//画一矩形并填充
	function fill($x, $y, $w = 0, $h = 0, $color = 'ffffff')
	{
		if($w == 0)
			$w = $this->sw - $w;

		if($h == 0)
			$h = $this->sh - $y;

		imagefilledrectangle($this->handle, $x, $y, $w - $x, $h - $y, $this->color2index($color));
		$this->allClear();
	}
//-------------------------------------------------------------------------------

	//由x,y轴开始覆盖图片obj
	function imagecopyAll($obj, $x, $y)
	{
		$obj->sw = $obj->sw ? $obj->sw : $obj->w;
		$obj->sh = $obj->sh ? $obj->sh : $obj->h;
		imagecopyresampled($this->handle, $obj->handle, $x, $y, 0, 0, $obj->sw, $obj->sh, $obj->sw, $obj->sh);
		$this->allClear();
	}
//---------------------------------------------------------------------------------------
	function resize($w, $h = 0)
	{
		if(empty($h))
			$h = $w;
		
		if(substr($w, -1) == '%')
		{
			$w = (int)$w;
			$w = $this->sw * $w / 100;
		}
		
		if(substr($h, -1) == '%')
		{
			$h = (int)$h;
			$h = $this->sh * $h / 100;
		}
		
		$handle = imagecreatetruecolor($w, $h);
		$ff = imagecolorallocate($handle, 255, 255, 255);
		imagecopyresampled($handle, $this->handle, 0, 0, 0, 0, $w, $h, $this->sw, $this->sh);
		$this->init($handle);
		$this->allClear();
	}
}